package com.icinfo.cloud.provider.punish.strategy.document;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.punish.document.DocumentFieldCustomConverter;
import com.icinfo.cloud.provider.punish.document.annotations.DocumentParamConvert;
import com.icinfo.cloud.provider.punish.document.dto.PrintDocumentBuildDto;
import com.icinfo.cloud.provider.punish.print.vo.PrintDocumentVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 询问通知书文书参数
 *
 * @author Junfeng.Chen
 * @date 2024/1/15 8:49
 **/
@Data
@Slf4j
public class XwtzsDocumentParam implements BaseDocumentParam {

    /**
     * 单位名称
     */
    @DocumentParamConvert(narrow = "orgStandardName", standard = "orgStandardName")
    private String orgStandardName;

    /**
     * 文号
     */
    @DocumentParamConvert(narrow = "documentNumber", standard = "documentNumber")
    private String documentNumber;


    /**
     * 当事人
     */
    @DocumentParamConvert(narrow = "partyName", standard = "askName")
    private String partyName;

    /**
     * 案发日期
     */
    @DocumentParamConvert(narrow = "xwtzs_afrq")
    private String eventDate;

    /**
     * 案发地址
     */
    @DocumentParamConvert(narrow = "addressWithLatAndLgt")
    private String address;

    /**
     * 案由
     */
    @DocumentParamConvert(narrow = "caseReasonFormat")
    private String caseReasonFormat;


    /**
     * 事由
     */
    @DocumentParamConvert(standard = "caseReason")
    private DocumentFieldCustomConverter caseReason = params -> {
        // 当事人
        String partyName = String.valueOf(Optional.ofNullable(params.get("partyName")).orElse(""));
        // 案发日期
        String eventDate = String.valueOf(Optional.ofNullable(params.get("xwtzs_afrq")).orElse(""));
        // 案发地址
        JSONObject object = JSONObject.parseObject(String.valueOf(params.get("addressWithLatAndLgt")));
        String address = Optional.ofNullable(object.getString("address")).orElse("");
        // 案由
        String caseReasonFormat = String.valueOf(Optional.ofNullable(params.get("caseReasonFormat")).orElse(""));
        return partyName + "于" + eventDate + "在" + address + "涉嫌" + caseReasonFormat;
    };


    /**
     * 询问日期
     */
    @DocumentParamConvert(narrow = "xwtzs_jsxwsj", standard = "askTime")
    private String askTime;

    /**
     * 询问地址
     */
    @DocumentParamConvert(narrow = "xwtzs_xwdz", standard = "acceptInquireAddress")
    private String acceptInquireAddress;

    /**
     * 标准版 主联系人
     */
    @DocumentParamConvert(narrow = "mainHandleUser", standard = "mainHandlePerson")
    private String mainHandlePerson;

    /**
     * 标准版 副联系人
     */
    @DocumentParamConvert(narrow = "assistHandleUser", standard = "helpHandlePerson")
    private String helpHandlePerson;

    /**
     * 标准版 联系电话
     */
    @DocumentParamConvert(narrow = "deptLinkPhone", standard = "orgPhone")
    private String orgPhone;

    /**
     * 营业执照
     */
    @DocumentParamConvert(narrow = "xwtzs_yyzz")
    private String businessLicense;

    /**
     * 组织机构代码
     */
    @DocumentParamConvert(narrow = "xwtzs_zzjgdmz")
    private String orgCode;

    /**
     * 法定代表人或负责人身份证及身份证明书
     */
    @DocumentParamConvert(narrow = "xwtzs_fddbrzs")
    private String legalIdentityCard;

    /**
     * 本人身份证或其他有效证件
     */
    @DocumentParamConvert(narrow = "xwtzs_brsfzj")
    private String personIdentityCard;

    /**
     * 授权委托书
     */
    @DocumentParamConvert(narrow = "xwtzs_sqwts")
    private String letterAuthorization;

    /**
     * 相关许可证件
     */
    @DocumentParamConvert(narrow = "xwtzs_xgxkzj")
    private String relevantLicense;

    /**
     * 其他材料
     */
    @DocumentParamConvert(narrow = "xwtzs_otherfile")
    private String otherFile;

    /**
     * 其他材料-详情
     */
    @DocumentParamConvert(narrow = "XWTZS_ZF_QTCL_XQ", standard = "otherFile")
    private String narrowOtherFileDetails;
    /**
     * 宽幅的 选择信息
     */
    @DocumentParamConvert(standard = "acceptInquireCarryMaterial")
    private DocumentFieldCustomConverter acceptInquireCarryMaterial = params ->{
        List<String> list = new ArrayList<>();
        // 营业执照
        Boolean businessLicense = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_yyzz")).orElse("false")));
        if (businessLicense){
            list.add("营业执照");
        }
        // 组织机构代码证
        Boolean orgCode = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_zzjgdmz")).orElse("false")));
        if (orgCode){
            list.add("组织机构代码证");
        }
        // 法定代表人或负责人身份证及身份证明书
        Boolean legalIdentityCard = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_fddbrzs")).orElse("false")));
        if (legalIdentityCard){
            list.add("法定代表人或负责人身份证及身份证明书");
        }
        // 本人身份证或其他有效证件
        Boolean personIdentityCard = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_brsfzj")).orElse("false")));
        if (personIdentityCard){
            list.add("本人身份证或其他有效证件");
        }
        // 授权委托书
        Boolean letterAuthorization = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_sqwts")).orElse("false")));
        if (letterAuthorization){
            list.add("授权委托书");
        }
        // 相关许可证明文件
        Boolean relevantLicense = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_xgxkzj")).orElse("false")));
        if (relevantLicense){
            list.add("相关许可证明文件");
        }
        // 其他材料
        Boolean otherFile = Boolean.valueOf(String.valueOf(Optional.ofNullable(params.get("xwtzs_otherfile")).orElse("false")));
        if (otherFile){
            list.add("其他材料 " + Optional.ofNullable(params.get("XWTZS_ZF_QTCL_XQ")).orElse(""));
        }
        String acceptInquireCarryMaterial = String.join(",", list);
        log.info("acceptInquireCarryMaterial -> {}", acceptInquireCarryMaterial);
        return StrUtil.isNotBlank(acceptInquireCarryMaterial) ? acceptInquireCarryMaterial : "";
    };


    /**
     * 主办人员
     */
    @DocumentParamConvert(narrow = "mainHandleUser", standard = "mainLinkPerson")
    private String mainHandleUser;

    /**
     * 协办人员
     */
    @DocumentParamConvert(narrow = "assistHandleUser", standard = "assistLikePerson")
    private String assistHandleUser;

    /**
     * 联系电话
     */
    @DocumentParamConvert(narrow = "deptLinkPhone", standard = "linkPhone")
    private String deptLinkPhone;

    /**
     * 当事人签字
     */
    @DocumentParamConvert(narrow = "signFingerprintPic", standard = "signFingerprintPic")
    private String signFingerprintPic;

    /**
     * 签字时间
     */
    @DocumentParamConvert(narrow = "signFingerprintDate", standard = "signFingerprintDate")
    private String signFingerprintDate;

    /**
     * 窄幅-盖章机构
     */
    @DocumentParamConvert(narrow = "orgStandardNameSeal")
    private String orgStandardNameSeal;

    /**
     * 盖章时间
     */
    @DocumentParamConvert(narrow = "sealDate", standard = "sealDate")
    private String sealDate;

    /**
     * 不签字理由
     */
    @DocumentParamConvert(narrow = "notSignReason", standard = "notSignReason")
    private String notSignReason;


    @Override
    public PrintDocumentVo assemblyPrintContent(PrintDocumentBuildDto dto) {
        //该文书较复杂，不能使用json， 图片打印暂不处理，后期有需求后对接
        return new PrintDocumentVo(null, null);
    }
}
